M
Hi all! I glad to show my template of project Hello world that uses CMake build system, it's named as myapptemplate.
My last sample was use GNU Autotools, it's a difficult system. Today we have many projects with CMake (cmake.org). For new projects I recommend use CMake. But it will have problems with cross-compilation for beginners. Let's find it out!
Step 1. Creating you own CMake project
First you have to install git and cmake applications in the host if there are none.
Next follow into you projects directory in the host, e.g. $HOME/projects and clone there my CMake template myapptemplate project. Then enter new project directory and remove .git dir from there.
git clone --single-branch --branch CMake https://bitbucket.org/hlorka/myapptemplate.git <my_app_name>
cd ./<my_app_name>
rm -rf ./.git
The files CMakeSettings.json and myapptemplate.vcxproj for MS Visual Studio IDE and can be deleted too.
Step 2A. General compilation for the host machine
It's very simple. First you have to configure and make cache CMake's files. Look at the command below:
cmake . [--config Debug]
--config Debug will append debug information (symbols) and postfix -d to the output binary file (helloworld-d).
Then you can build (compile) project, using next commands below
Via CMake:
cmake --build . [--clean-first]
Or via Makefile (make utility)
make
Step 2B. Cross-compilation using OpenWRT SDK
First open file cross-compile/openwrt-mips-toolchain.cmake for modify.
nano ./cross-compile/openwrt-mips-toolchain.cmake
Fragment of this file below:
...
# Локальные переменные
set(openwrt_sdk_dir $ENV{HOME}/OpenWRT-sdk)
# Экспортируемая переменная, в скрипт `xcmake.sh`
set(staging_dir ${openwrt_sdk_dir}/staging_dir)
set(toolchain_dir ${staging_dir}/toolchain-mipsel_24kc_gcc-7.3.0_musl)
...
You have to check/modify OpenWRT SDK paths.
Next run cmake with parameters:
cmake -DCMAKE_TOOLCHAIN_FILE=./cross-compile/openwrt-mips-toolchain.cmake [--config Debug] [-DREMOTE_INSTALL_IP=<remote-IP> -DREMOTE_INSTALL_USER=root]
Parameters REMOTE_INSTALL_* will make targets to help you copy binary files to the Omega via rsync. You also need SSH access by key.
To build (cross-compile) project you have to run script:
./xcmake.sh --build . [--clean-first]
Step 3 (optional). Remote-installation
If you set REMOTE_INSTALL_* parameters, after build you able using remote-install, remote-uninstall targets for example below:
cmake --build . --target remote-install
or
make remote-install
Step 4 (optional). Making IPK-package
After cross-compilation you will see files openwrt-manifest-git.mk, openwrt-manifest-local.mk in the root project directory. There are two variants of manifest (renamed Makefile).
openwrt-manifest-git.mk - variant of manifest file of project to make package that sources will be download from Git repository.
openwrt-manifest-local.mk - variant of manifest file of project to make package localy. It's useful for debugging or internal using.
To make package, first you have to create feed directory outside this project, e.g. $HOME/my-openwrt-packages. Create sub-directory in it with you project name, e.g. $HOME/my-openwrt-packages/<my_app_name>
Then make symlink there for one of the Manifest file, e.g.:
ln -sf $HOME/projects/<my_app_name>/openwrt-manifest-local.mk $HOME/my-openwrt-packages/<my_app_name>/Makefile
Next change working dir to the root of OpenWRT SDK. Open file feeds.conf.default in any text editor and append your own feed there like below string.
...
src-link my_packages /home/<my_user_name>/projects/my-openwrt-packages
Then run once sequence of commands:
./scripts/feeds update my_packages
./scripts/feeds install <my_app_name>
Now you can build IPK-package:
Variant openwrt-manifest-local.mk:
make package/<my_app_name>/prepare
make package/<my_app_name>/compile
Variant openwrt-manifest-git.mk:
make package/<my_app_name>/download
make package/<my_app_name>/compile
Your IPK-files you can find in the directory <OpenWRT_SDK_dir>/bin/packages/mipsel_24kc/my_packages. It will named like <my_app_name>_1.0-1_mipsel_24kc.ipk
To install package from IPK-file in the Omega, run next command:
opkg install ./<my_app_name>_1.0-1_mipsel_24kc.ipk